package com.baseproject.helper;

import android.annotation.SuppressLint;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import com.tmall.wireless.tangram.structure.BaseCell;

import org.json.JSONArray;
import org.json.JSONException;

/**
 * Created by hp on 2017/7/14.
 */

public class ViewHelper {

    @SuppressLint("NewApi")
    public static void setSelectableItemBackground(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            TypedValue typedValue = new TypedValue();
            view.getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
            int[] attribute = new int[]{android.R.attr.selectableItemBackground};
            TypedArray typedArray = view.getContext().getTheme().obtainStyledAttributes(typedValue.resourceId, attribute);
            view.setBackground(typedArray.getDrawable(0));
        }
    }

    public static void setPaddingFromCell(BaseCell cell, ViewGroup viewGroup) {
        JSONArray jsonArrayPadding = cell.optJsonArrayParam("padding");
        if (null != jsonArrayPadding && jsonArrayPadding.length() > 0) {
            try {
                viewGroup.setPadding(jsonArrayPadding.getInt(3), jsonArrayPadding.getInt(0), jsonArrayPadding.getInt(1), jsonArrayPadding.getInt(2));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
